package day15.socketdemo07;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Xiao
 * @date 2021/8/9
 */
public class ServerDemo07_1 {
    /**
     * 需求：
     * 客户端：将本地文件上传到服务器，接收服务器的反馈；
     * 服务器：接收客户端上传的文件，上传完毕之后给出反馈。
     */
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10000);
        Socket so = ss.accept();
        //创建服务器输入流对象从客户端读入文件，创建本地输出流对象将文件写出到本地
        BufferedReader br = new BufferedReader(new InputStreamReader(so.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new FileWriter(
                "videoPractice\\src\\day15\\socketdemo07\\demo07Copy.txt"));
        String s;
        while ((s = br.readLine()) != null) {
            System.out.println(s);
            bw.write(s);
            bw.newLine();
        }
        //写完一定要调用flush()方法刷新流否则没有数据写出
        bw.flush();
        //创建服务器输出流对象，反馈信息给客户端
        BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(so.getOutputStream()));
        bw1.write("已经完成上传工作，Over！");
        bw1.newLine();
        bw1.flush();
        //关闭本地IO流和服务器Socket对象
        bw.close();
        so.close();
        ss.close();
    }
}
